package com.dingreading.cloud.common.util.aliyun;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 阿里云短信
 */
public class AliyunMessageUtils {

    /**
     * 验证码模板编号
     */
    private static final String TEMPLATE_VERIFY_CODE = "SMS_186361144";

    /**
     * 成功预约观展门票
     */
    private static final String TEMPLATE_TICKET_FEE = "SMS_478595845";

    /**
     * 发送验证码
     */
    public static String acecSendVerifyCode(
            String accessKey, String accessSecret, String phone, String code
    ) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        request.putQueryParameter("PhoneNumbers", phone);
        // 签名
        request.putQueryParameter("SignName", "ACEC大会");
        // 模板编号
        request.putQueryParameter("TemplateCode", TEMPLATE_VERIFY_CODE);
        // 短信模板变量
        JSONObject paramObj = new JSONObject();
        paramObj.put("code", code);
        request.putQueryParameter("TemplateParam", paramObj.toJSONString());

        CommonResponse response = client.getCommonResponse(request);
        return response.getData();
    }

    /**
     * 发送成功预约观展门票
     */
    public static String acecSendTicketFee(
            String accessKey, String accessSecret, String phone, String uid
    ) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        request.putQueryParameter("PhoneNumbers", phone);
        // 签名
        request.putQueryParameter("SignName", "ACEC大会");
        // 模板编号
        request.putQueryParameter("TemplateCode", TEMPLATE_TICKET_FEE);
        // 短信模板变量
        JSONObject paramObj = new JSONObject();
        paramObj.put("uid", uid);
        request.putQueryParameter("TemplateParam", paramObj.toJSONString());

        CommonResponse response = client.getCommonResponse(request);
        return response.getData();
    }

}
